import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class MemberCashOutService extends BaseService<any> {
  private readonly logger = new Logger(MemberCashOutService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: MemberCashOutService_admin::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $field = 'id,member_cash_out.site_id,cash_out_no,member_cash_out.member_id,account_type,transfer_type,transfer_realname,transfer_mobile,transfer_bank,...
const field = 'id,member_cash_out.site_id,cash_out_no,member_cash_out.member_id,account_type,transfer_type,transfer_realname,transfer_mobile,transfer_bank,transfer_account,transfer_fail_reason,transfer_status,transfer_time,apply_money,rate,service_money,member_cash_out.money,audit_time,member_cash_out.status,member_cash_out.remark,member_cash_out.create_time,refuse_reason,transfer_no, transfer_payment_code';
        const member_where = [];
        if(!!where.keywords))
        {
            const member_where = [['member.member_no|member.nickname|member.mobile|member.username', '=', where.keywords]];
  }

  /**
   * getInfo
   * 对应 PHP: MemberCashOutService_admin::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(id: number) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: $field = 'id,site_id,cash_out_no,member_id,account_type,transfer_type,transfer_realname,transfer_mobile,transfer_bank,transfer_account,transfer_fail_r...
const field = 'id,site_id,cash_out_no,member_id,account_type,transfer_type,transfer_realname,transfer_mobile,transfer_bank,transfer_account,transfer_fail_reason,transfer_status,transfer_time,apply_money,rate,service_money,money,audit_time,status,remark,create_time,refuse_reason,transfer_no, transfer_payment_code';
        return this.model.where([['id', '=', id], ['site_id', '=', this.site_id))).with(['memberInfo', 'transfer')).field(field).append(['status_name', 'transfer_status_name', 'transfer_type_name', 'account_type_name')).findOrEmpty().toArray(];
    }

    /**
     * @param int id
     * @param string action
     * @param data
     * @return true|null
     */
    async audit(int id, string action, data){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.audit(this.site_id, id, action, data);
    }


    /**
     * 转账
     * @param int id
     * @param data
     * @return true
     */
    async transfer(int id, data){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        const cash_out = core_member_cash_out_service.find(this.site_id, id);
        if (cash_out.isEmpty()) throw new BusinessException('RECHARGE_LOG_NOT_EXIST');
        if (cash_out.status != MemberCashOutDict.WAIT_TRANSFER && cash_out.transfer_type == TransferDict.WECHAT) throw new BusinessException('CASH_OUT_WECHAT_ACCOUNT_NOT_ALLOW_ADMIN'];
        return core_member_cash_out_service.transfer(this.site_id, id, data);
    }

    /**
     * 备注
     * @param int id
     * @param data
     * @return true
     */
    async remark(int id, data){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.remark(this.site_id, id, data);
    }
    /**
     * 统计数据
     * @return */
    async stat()
    {
        const stat = [];
        //已提现
        stat.transfered = this.model.where([['status', '=', MemberCashOutDict.TRANSFERED], ['site_id', '=', this.site_id))).sum("apply_money"];
        //所有金额（包括提现中，已提现）
        const all_money = this.model.where([['status', '>=', 0], ['site_id', '=', this.site_id))).sum("apply_money");

        stat.cash_outing = all_money - stat.transfered;
        return stat;
    }

    /**
     * 检测实际的转账状态
     * @param int id
     * @return true
     */
    async checkTransferStatus(int id){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.checkTransferStatus(this.site_id, id);
    }

    /**
     * 取消体现
     * @param int id
     * @return true|null
     */
    async cancel(int id){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.cancel(this.site_id, id);
    }

}
  }

  /**
   * audit
   * 对应 PHP: MemberCashOutService_admin::audit()
   * 逻辑类型: undefined - undefined
   */
  async audit(id: number, action: number, data: number) {
    // 基于PHP真实逻辑: audit
    // PHP原文: $core_member_cash_out_service = new CoreMemberCashOutService();         return $core_member_cash_out_service->audit($this->site_id, $id, $action, $dat...
const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.audit(this.site_id, id, action, data);
    }


    /**
     * 转账
     * @param int id
     * @param data
     * @return true
     */
    async transfer(int id, data){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        const cash_out = core_member_cash_out_service.find(this.site_id, id);
        if (cash_out.isEmpty()) throw new BusinessException('RECHARGE_LOG_NOT_EXIST');
        if (cash_out.status != MemberCashOutDict.WAIT_TRANSFER && cash_out.transfer_type == TransferDict.WECHAT) throw new BusinessException('CASH_OUT_WECHAT_ACCOUNT_NOT_ALLOW_ADMIN'];
        return core_member_cash_out_service.transfer(this.site_id, id, data);
    }

    /**
     * 备注
     * @param int id
     * @param data
     * @return true
     */
    async remark(int id, data){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.remark(this.site_id, id, data);
    }
    /**
     * 统计数据
     * @return */
    async stat()
    {
        const stat = [];
        //已提现
        stat.transfered = this.model.where([['status', '=', MemberCashOutDict.TRANSFERED], ['site_id', '=', this.site_id))).sum("apply_money"];
        //所有金额（包括提现中，已提现）
        const all_money = this.model.where([['status', '>=', 0], ['site_id', '=', this.site_id))).sum("apply_money");

        stat.cash_outing = all_money - stat.transfered;
        return stat;
    }

    /**
     * 检测实际的转账状态
     * @param int id
     * @return true
     */
    async checkTransferStatus(int id){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.checkTransferStatus(this.site_id, id);
    }

    /**
     * 取消体现
     * @param int id
     * @return true|null
     */
    async cancel(int id){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.cancel(this.site_id, id);
    }

}
  }

  /**
   * transfer
   * 对应 PHP: MemberCashOutService_admin::transfer()
   * 逻辑类型: undefined - undefined
   */
  async transfer(id: any[], data: any[]) {
    // 基于PHP真实逻辑: transfer
    // PHP原文: $core_member_cash_out_service = new CoreMemberCashOutService();         $cash_out = $core_member_cash_out_service->find($this->site_id, $id);         ...
const core_member_cash_out_service = this.coreMemberCashOutService;
        const cash_out = core_member_cash_out_service.find(this.site_id, id);
        if (cash_out.isEmpty()) throw new BusinessException('RECHARGE_LOG_NOT_EXIST');
        if (cash_out.status != MemberCashOutDict.WAIT_TRANSFER && cash_out.transfer_type == TransferDict.WECHAT) throw new BusinessException('CASH_OUT_WECHAT_ACCOUNT_NOT_ALLOW_ADMIN'];
        return core_member_cash_out_service.transfer(this.site_id, id, data);
    }

    /**
     * 备注
     * @param int id
     * @param data
     * @return true
     */
    async remark(int id, data){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.remark(this.site_id, id, data);
    }
    /**
     * 统计数据
     * @return */
    async stat()
    {
        const stat = [];
        //已提现
        stat.transfered = this.model.where([['status', '=', MemberCashOutDict.TRANSFERED], ['site_id', '=', this.site_id))).sum("apply_money"];
        //所有金额（包括提现中，已提现）
        const all_money = this.model.where([['status', '>=', 0], ['site_id', '=', this.site_id))).sum("apply_money");

        stat.cash_outing = all_money - stat.transfered;
        return stat;
    }

    /**
     * 检测实际的转账状态
     * @param int id
     * @return true
     */
    async checkTransferStatus(int id){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.checkTransferStatus(this.site_id, id);
    }

    /**
     * 取消体现
     * @param int id
     * @return true|null
     */
    async cancel(int id){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.cancel(this.site_id, id);
    }

}
  }

  /**
   * remark
   * 对应 PHP: MemberCashOutService_admin::remark()
   * 逻辑类型: undefined - undefined
   */
  async remark(id: any[], data: any[]) {
    // 基于PHP真实逻辑: remark
    // PHP原文: $core_member_cash_out_service = new CoreMemberCashOutService();         return $core_member_cash_out_service->remark($this->site_id, $id, $data);     ...
const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.remark(this.site_id, id, data);
    }
    /**
     * 统计数据
     * @return */
    async stat()
    {
        const stat = [];
        //已提现
        stat.transfered = this.model.where([['status', '=', MemberCashOutDict.TRANSFERED], ['site_id', '=', this.site_id))).sum("apply_money"];
        //所有金额（包括提现中，已提现）
        const all_money = this.model.where([['status', '>=', 0], ['site_id', '=', this.site_id))).sum("apply_money");

        stat.cash_outing = all_money - stat.transfered;
        return stat;
    }

    /**
     * 检测实际的转账状态
     * @param int id
     * @return true
     */
    async checkTransferStatus(int id){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.checkTransferStatus(this.site_id, id);
    }

    /**
     * 取消体现
     * @param int id
     * @return true|null
     */
    async cancel(int id){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.cancel(this.site_id, id);
    }

}
  }

  /**
   * stat
   * 对应 PHP: MemberCashOutService_admin::stat()
   * 逻辑类型: undefined - undefined
   */
  async stat() {
    // 基于PHP真实逻辑: stat
    // PHP原文: $stat = [];         //已提现         $stat['transfered'] = $this->model->where([['status', '=', MemberCashOutDict::TRANSFERED], ['site_id', '=', $this->s...
const stat = [];
        //已提现
        stat.transfered = this.model.where([['status', '=', MemberCashOutDict.TRANSFERED], ['site_id', '=', this.site_id))).sum("apply_money"];
        //所有金额（包括提现中，已提现）
        const all_money = this.model.where([['status', '>=', 0], ['site_id', '=', this.site_id))).sum("apply_money");

        stat.cash_outing = all_money - stat.transfered;
        return stat;
    }

    /**
     * 检测实际的转账状态
     * @param int id
     * @return true
     */
    async checkTransferStatus(int id){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.checkTransferStatus(this.site_id, id);
    }

    /**
     * 取消体现
     * @param int id
     * @return true|null
     */
    async cancel(int id){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.cancel(this.site_id, id);
    }

}
  }

  /**
   * checkTransferStatus
   * 对应 PHP: MemberCashOutService_admin::checkTransferStatus()
   * 逻辑类型: undefined - undefined
   */
  async checkTransferStatus(id: number) {
    // 基于PHP真实逻辑: checkTransferStatus
    // PHP原文: $core_member_cash_out_service = new CoreMemberCashOutService();         return $core_member_cash_out_service->checkTransferStatus($this->site_id, $id)...
const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.checkTransferStatus(this.site_id, id);
    }

    /**
     * 取消体现
     * @param int id
     * @return true|null
     */
    async cancel(int id){
        const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.cancel(this.site_id, id);
    }

}
  }

  /**
   * cancel
   * 对应 PHP: MemberCashOutService_admin::cancel()
   * 逻辑类型: undefined - undefined
   */
  async cancel(id: number) {
    // 基于PHP真实逻辑: cancel
    // PHP原文: $core_member_cash_out_service = new CoreMemberCashOutService();         return $core_member_cash_out_service->cancel($this->site_id, $id);     }  }...
const core_member_cash_out_service = this.coreMemberCashOutService;
        return core_member_cash_out_service.cancel(this.site_id, id);
    }

}
  }
}
